#include "Camera.h"
#include <iostream>

Camera::Camera(Input *receiver, ISceneManager *smgr) {

	_receiver = receiver;
	_camera = smgr->addCameraSceneNode(0, vector3df(0,200,0), vector3df(0,-1,0));
    _moveX = 0;
        _moveY = 0;
        _moveZ = 0;
}

void Camera::update(irr::u32 current_time, irr::u32 last_time)
{
	core::vector3df pos = _camera->getPosition(); //Camera.getPosition();
	core::vector3df trg = _camera->getTarget(); //Camera.getPosition();
        irr::u32 time_factor = (current_time-last_time);
        if (_receiver->keys[irr::KEY_KEY_W]) //X++
        {
                if(_moveX < 0)
                    _moveX +=0.01/time_factor;
                if(_moveX < 1)
                    _moveX +=0.01/time_factor;
                else
                    _moveX = 1;

	}
        else if (_receiver->keys[irr::KEY_KEY_S]) //X--
        {
                if(_moveX > 0)
                    _moveX -=0.01/time_factor;
                if(_moveX > -1)
                    _moveX -=0.01/time_factor;
                else
                    _moveX = -1;
	}
        else
        {
            if(_moveX != 0)
            {
                if(_moveX < 0)
                    _moveX +=0.01/time_factor;
                else
                    _moveX -=0.01/time_factor;
            }
        }



	if (_receiver->keys[irr::KEY_KEY_A]) //Z++
        {
                if(_moveZ < 0)
                    _moveZ +=0.01/time_factor;
                if(_moveZ < 1)
                    _moveZ +=0.01/time_factor;
                else
                    _moveZ = 1;
	}
        else if (_receiver->keys[irr::KEY_KEY_D]) //Z--
	{
                if(_moveZ > 0)
                    _moveZ -=0.01/time_factor;
                if(_moveZ > -1)
                    _moveZ -=0.01/time_factor;
                else
                    _moveZ = -1;
	}
        else
        {
            if(_moveZ != 0)
            {
                if(_moveZ < 0)
                    _moveZ +=0.01/time_factor;
                else
                    _moveZ -=0.01/time_factor;
            }
        }



	if (_receiver->keys[irr::KEY_KEY_E]) //Y++
        {
                if(_moveY < 0)
                    _moveY +=0.01/time_factor;
                if(_moveY < 1)
                    _moveY +=0.01/time_factor;
                else
                    _moveY = 1;
	}
        else if (_receiver->keys[irr::KEY_KEY_Q])
        {
                if(_moveY > 0)
                    _moveY -=0.01/time_factor;
                if(_moveY > -1)
                    _moveY -=0.01/time_factor;
                else
                    _moveY = -1;
	}
        else
        {
            if(_moveY != 0)
            {
                if(_moveY < 0)
                    _moveY +=0.01/time_factor;
                else
                    _moveY -=0.01/time_factor;
            }
        }

        f32 _moveX2 = _moveX * (pos.Y/100 + 1);
        f32 _moveZ2 = _moveZ * (pos.Y/100 + 1);
        f32 _moveY2 = _moveY * (pos.Y/100 + 1);
      

        pos += core::vector3df(2*_moveX2, 2*_moveY2, 2*_moveZ2)/time_factor;
        trg += core::vector3df(2*_moveX2, 0, 2*_moveZ2)/time_factor;

        if (pos.Y < 10)
        {
            pos.Y = 10;
            _moveY=-_moveY;
        }
	_camera->setPosition(pos);
	_camera->setTarget(trg);
}

ISceneNode* Camera::getSceneNode()
{
	return _camera;
}
